package com.v2future.sjt.collegecheckingattendance.fruit.research;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ConditionBean;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ConditionResponeBean;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ConditionTypeBean;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ResearchTypeBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;
import com.v2future.sjt.collegecheckingattendance.widget.view.PullToRefreshView;

import java.util.List;

/**
 * 一种科研类型列表
 */
public class OneResearchTypeListActivity extends MainActivity implements PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener{

    PullToRefreshView pullView;
    ListView fruitLv;
    List<ConditionBean> dataList;
    ResearchAdapter adapter;

    /**
     * 成长类型
     */

    private ConditionTypeBean conditionTypeBean;

    /**
     * 科研类型
     */
    private ResearchTypeBean researchTypeBean;

    private int totalPage = 0;//总页数

    private int currentPage = 0;//当前页数

    /**
     * init first
     */
    @Override
    public void init() {
        conditionTypeBean = (ConditionTypeBean) getIntent().getSerializableExtra(IntentKey.FRUIT_TYPE);
        researchTypeBean = (ResearchTypeBean) getIntent().getSerializableExtra(IntentKey.RESEARCH_TYPE);
    }

    /**
     * init view
     */
    @Override
    public void initView() {
        setContentView(R.layout.activity_fruit_one_type);
        initTitleBack(null);
        setTitleText(researchTypeBean.getResearchSubjectName());
        setRightText(getString(R.string.add), addListener);
        pullView = (PullToRefreshView) findViewById(R.id.pull_view);
        pullView.setOnHeaderRefreshListener(this);
        pullView.setOnFooterRefreshListener(this);
        fruitLv = (ListView) findViewById(R.id.lv_fruit);

    }

    /**
     * init after
     */
    @Override
    public void initAfter() {


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != conditionTypeBean) {
            showTitleLoading();
            doGetUserCondition(currentPage);
        }
        else {
            tip("类型错误");
        }
    }

    private void refreshView(ConditionResponeBean result){
        if (currentPage <= 1) {
            dataList = result.getData();
            adapter = new ResearchAdapter(dataList, this);
            adapter.setConditionTypeBean(conditionTypeBean);
            adapter.setResearchTypeBean(researchTypeBean);
            fruitLv.setAdapter(adapter);
            fruitLv.setOnItemClickListener(onItemClickListener);
        }
        else {
            dataList.addAll(result.getData());
            adapter.notifyDataSetChanged();
        }
    }

    private void doGetUserCondition(final int page) {
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.USER_CONDITION));
        params.addQueryStringParameter("conditonTypeId", conditionTypeBean.getConditonTypeId());
        params.addQueryStringParameter("researchSubjectId", researchTypeBean.getResearchSubjectId());
        if (page > 1) {
            params.addQueryStringParameter("page", page+"");
        }
        NetUtils.doGetRequest(this, params, new NetTaskListener<ConditionResponeBean>() {

            /**
             * 数据获取完成
             *
             * @param result
             */
            @Override
            public void onSuccess(ConditionResponeBean result) {
                if (null != result.getData()) {
                    try {
                        currentPage = Integer.parseInt(result.getPage());
                        totalPage = Integer.parseInt(result.getTotalPage());
                    }
                    catch (Exception e){}
                    refreshView(result);
                }
            }

            /**
             * 位置网络错误
             */
            @Override
            public void onError() {

            }

            /**
             * 非正常状态的数据
             *
             * @param result
             */
            @Override
            public void onInterError(ConditionResponeBean result) {
                tip(result.getFailReason());
            }

            /**
             * 网络错误
             *
             * @param responseCode
             * @param responseMsg
             * @param errorResult
             */
            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            /**
             * 请求取消
             */
            @Override
            public void onCancelled() {
                NetUtils.cancelRequest();
            }

            /**
             * 网络请求流程结束
             */
            @Override
            public void onFinished() {
                cancelTitleLoading();
                cancelLoadingDialog();
                pullView.onHeaderRefreshComplete();
                pullView.onFooterRefreshComplete();
            }

            /**
             * 缓存获取完成
             *
             * @param result
             */
            @Override
            public void onSuccessCache(ConditionResponeBean result) {

            }
        });
    }

    private View.OnClickListener addListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(OneResearchTypeListActivity.this, AddOneResearchTypeActivity.class);
            intent.putExtra(IntentKey.RESEARCH_TYPE, researchTypeBean);
            intent.putExtra(IntentKey.FRUIT_TYPE, conditionTypeBean);
            startActivity(intent);
        }
    };

    /**
     * 列表点击
     */
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ConditionBean bean = dataList.get(position);
            Intent intent = new Intent(OneResearchTypeListActivity.this, ResearchDetailActivity.class);
            intent.putExtra(IntentKey.FRUIT_TYPE, conditionTypeBean);
            intent.putExtra(IntentKey.RESEARCH_TYPE, researchTypeBean);
            intent.putExtra(IntentKey.CONDITION, bean);
            startActivity(intent);
        }
    };

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        if (totalPage == currentPage) {
            pullView.onFooterRefreshComplete();
            tip("已到尾页");
            return;
        }
        currentPage++;
        doGetUserCondition(currentPage);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        currentPage = 1;
        doGetUserCondition(currentPage);
    }
}
